package com.example.mytayunapi.Service;

import com.example.mytayunapi.Entity.userInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class TokenServiceImpl implements TokenService {

    private static final Logger logger = LoggerFactory.getLogger(TokenServiceImpl.class);
    private static final String SECRET_KEY = "youra256abitasecretyoura256abitasecretyoura256abitasecretyoura256abitasecretyoura256abitasecretyoura256abitasecret";

    @Override
    public String generateToken(userInfo user) {
        try {
            String subject = user.getSubjectForToken();
            return Jwts.builder()
                    .setSubject(subject)
                    .setIssuedAt(new Date())
                    .setExpiration(new Date(System.currentTimeMillis() + 3600000)) // 1小时后过期
                    .signWith(SignatureAlgorithm.HS256, SECRET_KEY)
                    .compact();
        } catch (Exception e) {
            logger.error("Token generation failed for user: " + user, e);
            return null;
        }
    }
}

